//
//  DXKeyBoardToolBar.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/15.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
enum KeyBoardToolBarType:Int{
//    最近表情
    case KeyBoardToolBarLatest = 0
//    默认表情
    case KeyBoardToolBarDefault = 1
//    Emoji表情
    case KeyBoardToolBarEmoji = 2
//    浪小花表情
    case KeyBoardToolBarLxh = 3
}

// 点击toolbar的协议
protocol DXKeyBoardToolBarDelegate : NSObjectProtocol{
 
    func keyBoardToolBarClick(buttonTyp:KeyBoardToolBarType)

}

class DXKeyBoardToolBar: UIStackView {

    
//      记录上次点击的button
    var lastClickButton:DXKeyBoardToolBarButton?
    
    var lastScrollBarType:KeyBoardToolBarType = .KeyBoardToolBarLatest
//     代理
    weak var delegate:DXKeyBoardToolBarDelegate?
//    储存toolbar的数组
    var toolbarButtons:[DXKeyBoardToolBarButton] = [DXKeyBoardToolBarButton]()
//     给外调用选中哪个button
    var selectedToolBarButton:KeyBoardToolBarType = .KeyBoardToolBarLatest {
        didSet{
        if selectedToolBarButton != lastScrollBarType {
            let index = selectedToolBarButton.rawValue
            for (i ,button) in toolbarButtons.enumerate(){
            button.selected = i == index ? true : false
    
            }
//            记录被点击的button
            lastClickButton = toolbarButtons[index]
        }
       lastScrollBarType = selectedToolBarButton
    }
}
//    重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        axis = .Horizontal
        distribution = .FillEqually
          setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 初始化UI
    private func setUpUI(){
       let latestBtn = addToolBarButton("compose_emotion_table_left", title: "最近", type:.KeyBoardToolBarLatest) as! DXKeyBoardToolBarButton
//        设置最近按钮为默认选中状态
        latestBtn.selected = true
//        记录上次选中按钮为默认
        lastClickButton = latestBtn
         addToolBarButton("compose_emotion_table_mid", title: "默认", type:.KeyBoardToolBarDefault)
         addToolBarButton("compose_emotion_table_mid", title: "Emoji", type:.KeyBoardToolBarEmoji)
         addToolBarButton("compose_emotion_table_right", title: "浪小花", type:.KeyBoardToolBarLxh)
    
    
    }
    
    // MARK: - 添加button
    private func addToolBarButton(bgimgName:String ,title:String,type:KeyBoardToolBarType)->(UIButton){
        let button = DXKeyBoardToolBarButton()
        button.tag = type.rawValue
        button.setBackgroundImage(UIImage(named: "\(bgimgName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgimgName)_selected"), forState: .Selected)
        button.setTitle(title, forState: .Normal)
        button.addTarget(self, action: "keyBoardToolBarButtonClick:", forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
//        添加到数组中
        toolbarButtons.append(button)
        addArrangedSubview(button)
        
        return button
    }
    
    // MARK: - ToolBarButton点击事件
    @objc private func keyBoardToolBarButtonClick(sender:DXKeyBoardToolBarButton){

        lastClickButton?.selected = false
        sender.selected = true
        lastClickButton = sender
//        代理方法的回调
        delegate?.keyBoardToolBarClick( KeyBoardToolBarType.init(rawValue: sender.tag)!)
       
        
    }

    
    
}



class DXKeyBoardToolBarButton: UIButton {

    // 将按钮的高度效果干掉
    override var highlighted: Bool {
        set{
            
        }
        get{
            return false
        }
    }
}
